package com.isman.placeslist.database;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class DataProvider extends ContentProvider {
	private static final int PEOPLE = 1;
	
    private UriMatcher uriMatcher;
    private DBHelper dbHelper;
    
	@Override
	public boolean onCreate() {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PeopleContent.AUTHORITY, "people", PEOPLE);

        dbHelper = new DBHelper(getContext());
		return true;
	}
	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db;
        Cursor cursor = null;
        if (uriMatcher.match(uri) == PEOPLE) {
            db = dbHelper.getReadableDatabase();
            cursor = db.query(PeopleContent.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
        }
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
	}
	@Override
	public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Uri newUri = null;
        if (uriMatcher.match(uri) == PEOPLE) {
            try {
                long id = db.insertOrThrow(PeopleContent.TABLE_NAME, null, values);
                newUri = ContentUris.withAppendedId(PeopleContent.CONTENT_URI, id);
                getContext().getContentResolver().notifyChange(newUri, null);
                Log.i("LISProject", "Insert data.");
            } catch (NullPointerException e) {
            	Log.v("Error","OnInsert"+e);
            }
        }
        return newUri;
	}
	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count;      
        switch (uriMatcher.match(uri)) {
        case PEOPLE:
            count = db.update(PeopleContent.TABLE_NAME, values, selection, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        
        // Notify any watchers of the change
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
	}
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count;   
        Log.v("Delete",selection);
        
        switch (uriMatcher.match(uri)) {
        	case PEOPLE:
        		count = db.delete(PeopleContent.TABLE_NAME, selection, selectionArgs);
        		break;
        	default:
        		throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
	}
	@Override
	public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case PEOPLE:
            return "com.android.isman.lis/com.android.isman.lis.PageMain";
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
	}
}

















////	    /** The MIME type of a directory of events */
////	    private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.example.brown";
////
////	    /** The MIME type of a single event */
////	    private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.example.brown";
////	    private static final String CONTENT_TYPE1 = "vnd.android.cursor.dir/vnd.example.brown";
////
////	    /** The MIME type of a single event */
////	    private static final String CONTENT_ITEM_TYPE1 = "vnd.android.cursor.item/vnd.example.brown";
//////	    private static final String CONTENT_TYPE2 = "vnd.android.cursor.dir/vnd.example.brown";
////
////	    /** The MIME type of a single event */
////	//  private static final String CONTENT_ITEM_TYPE2 = "vnd.android.cursor.item/vnd.example.brown";
//	    private DBHelper dbHelper;
//	    private UriMatcher uriMatcher;
//
//	    private Cursor cursor;
//	    private SQLiteDatabase db;
//	    String[] str;
//
//	    @Override
//	    public boolean onCreate() {
//
//	        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//	        uriMatcher.addURI(AUTHORITY, "user_deal", QUADUSER);
//	        uriMatcher.addURI(AUTHORITY, "user_deal/#", QUADUSER_ID);
//
//	        dbHelper = new DBHelper(getContext());
//	        return true;
//	    }
//
//	    @Override
//	    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) {
//	        SQLiteDatabase db;
//	        Cursor cursor = null;
//	        if (uriMatcher.match(uri) == QUADUSER_ID) {
//	            long id = Long.parseLong(uri.getPathSegments().get(1));
//	            selection = appendRowId(selection, id);
//	        }
//	        if (uriMatcher.match(uri) == QUADUSER) {
//	            // Get the database and run the query
//	            db = dbHelper.getReadableDatabase();
//	            cursor = db.query(People.TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy);
//	        }
//	        cursor.setNotificationUri(getContext().getContentResolver(), uri);
//	        return cursor;
//	    }
//
//	    @Override
//	    public String getType(Uri uri) {
//	        switch (uriMatcher.match(uri)) {
//	        case QUADUSER:
//	            return CONTENT_TYPE;
//	        case QUADUSER_ID:
//	            return CONTENT_ITEM_TYPE;
//	        default:
//	            throw new IllegalArgumentException("Unknown URI " + uri);
//	        }
//	    }
//
//	    @Override
//	    public Uri insert(Uri uri, ContentValues values) {
//
//	        SQLiteDatabase db = dbHelper.getWritableDatabase();
//	        Uri newUri = null;
//	        long id = -1;
//
//	        if (uriMatcher.match(uri) == QUADUSER) {
//	            try {
//
//	                id = db.insertOrThrow(People.TABLE_NAME, null, values);
//	                // Notify any watchers of the change
//	                newUri = ContentUris.withAppendedId(CONTENT_URI, id);
//	                getContext().getContentResolver().notifyChange(newUri, null);
//
//	            } catch (NullPointerException e) {
//	                    Log.v("Error","OnInsert"+e);
//	            }
//	        } 
//	        else if (uriMatcher.match(uri) != QUADUSER) {
//	            throw new IllegalArgumentException("Unknown URI bru press " + uri);
//	        }
//
//	        return newUri;
//	    }
//
//	    @Override
//	    public int delete(Uri uri, String selection, String[] selectionArgs) {
//	        SQLiteDatabase db = dbHelper.getWritableDatabase();
//	        int count;
//	        long id;        
//	        Log.v("Delete",selection);
//	        switch (uriMatcher.match(uri)) {
//
//	        case QUADUSER:
//	            count = db.delete(TABLE_NAME, selection, selectionArgs);
//	            break;
//	        case QUADUSER_ID:
//	            id = Long.parseLong(uri.getPathSegments().get(1));
//	            count = db.delete(People.TABLE_NAME, appendRowId(selection, id),
//	                    selectionArgs);
//	            break;
//	        default:
//	            throw new IllegalArgumentException("Unknown URI " + uri);
//	        }
//	        getContext().getContentResolver().notifyChange(uri, null);
//	        return count;
//	    }
//
//	    @Override
//	    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
//	        SQLiteDatabase db = dbHelper.getWritableDatabase();
//	        int count;
//	        long id;        
//	        switch (uriMatcher.match(uri)) {
//	        case QUADUSER:
//	            count = db.update(People.TABLE_NAME, values, selection, selectionArgs);
//	            break;
//	        case QUADUSER_ID:
//	            id = Long.parseLong(uri.getPathSegments().get(1));
//	            count = db.update(People.TABLE_NAME, values, appendRowId(selection, id),
//	                    selectionArgs);
//	            break;
//	        default:
//	            throw new IllegalArgumentException("Unknown URI " + uri);
//	        }
//
//	        // Notify any watchers of the change
//	        getContext().getContentResolver().notifyChange(uri, null);
//	        return count;
//	    }
//
//	    /** Append an id test to a SQL selection expression */
////	    private String appendRowId(String selection, long id) {
////	        return _ID
////	                + "="
////	                + id
////	                + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
////	                        : "");
////	    }
//
//
//}
